<template>
    <el-row :gutter="20" style="margin:20px 10px">
        <el-col :span="24">
            <el-form ref="form" label-width="80px" size="mini">
                <el-form-item label="收支时间" class="mt">
                    <el-date-picker v-model="fydate" type="datetime" placeholder="选择日期时间" @change="seldate" class="width"></el-date-picker>
                </el-form-item>
                <el-form-item label="收支分类">
                    <el-select v-model="costtypeid" placeholder="请选择收支类型" name="costtypeid" class="width">
                        <el-option v-for="(cafy,index) in category" :label="cafy.typename" :value="cafy.typename" :key="index"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="收支账户">
                    <el-select v-model="accountid" placeholder="请选择收支账户" class="width">
                        <el-option v-for="(gang,index) in accountlist" :label="gang.name" :value="gang.id" :key="index"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="收支金额">
                    <el-input type="number" v-model="costmoney" placeholder="请输入费用金额！" name="costmoney"></el-input>
                </el-form-item>
                <el-form-item label="收支描述">
                    <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="remarks"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submit">保存</el-button>
                    <el-button @click="close">取消</el-button>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>
</template>
<script>
import C from '@/common/common.js'

export default {
  name: 'addCost',
  data () {
    return {
      costtypeid: '',
      accountid: '',
      fydate: new Date(),
      costmoney: '',
      remarks: '',
      accountlist: [],
      category: [],
      pid: 0,
      ppid: 0
    }
  },
  methods: {
    getmsg: async function () {
      let row = C.getDatas()
      let data = await C.ajax('/shop/cost/addcost', row)
      if (data) {
        this.accountlist = data.acco
        this.category = data.cate
      }
    },
    seldate: function (val) {
      this.fydate = val
    },
    async submit () {
      let row = C.getDatas()
      if (!this.costtypeid) {
        C.error('请选择收支类别')
        return false
      } else if (!this.accountid) {
        C.error('请选择收款账户')
        return false
      } else if (this.costmoney === '' || this.costmoney <= 0) {
        C.error('请输入收支金额')
        return false
      } else if (this.fydate === '') {
        C.error('请选择添加费用的时间')
        return false
      }
      let ischaozhi = false
      this.accountlist.forEach((row) => {
        if (row.id == this.accountid) {
          if (this.costmoney > row.balance) {
            ischaozhi = true
          }
        }
      })
      if (ischaozhi && row.type == 2) {
        C.error('当前账户余额不足！')
        return false
      }
      for (var i = 0; i < this.accountlist.length; i++) {
        if (this.accountlist[i].id == this.accountid) {
          this.pid = this.accountlist[i].id
          for (var k = 0; k < this.category.length; k++) {
            if (this.category[k].typename == this.costtypeid) {
              var data = {
                costtypeid: this.category[k].id,
                costmoney: this.costmoney,
                type: row.type,
                remarks: this.remarks,
                accountid: this.pid,
                fydate: this.fydate
              }
              let res = await C.ajax('/shop/cost/addcost', data, 'post')
              C.closeBrowserWindow(res)
            }
          }
          return false
        }
      }
    },
    close () {
      C.closeBrowserWindow()
    }
  },
  mounted: function () {
    this.getmsg()
  }
}
</script>